package com.wdd.libbase.base

import android.content.Context
import androidx.multidex.MultiDex
import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.Utils
import com.wdd.libarouter.ARouterManager
import com.wdd.libbase.config.ModuleConfig
import com.wdd.libhttp.HttpManager
import com.wdd.libview.ViewManager
import dagger.android.DaggerApplication
import okhttp3.OkHttpClient
import javax.inject.Inject


/**
 * @ProjectName: OwnTool
 * @Package: com.wdd.modulebase.base
 * @ClassName: BaseApplication
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2019/11/28 15:25
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/11/28 15:25
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
abstract class BaseApplication : DaggerApplication() {
    @Inject
    lateinit var httpBuilder: OkHttpClient.Builder

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        MultiDex.install(base)
    }

    override fun onCreate() {
        super.onCreate()
        Utils.init(this)
        ARouterManager.initArouter(this)
        //请求初始化
        HttpManager.initHttp(this, httpBuilder.build())
        ViewManager.initView(this)
        modulesApplicationInit()
        LogUtils.getConfig().setLogSwitch(AppUtils.isAppDebug())
            .globalTag = "libBase"

        // AppWatcher.config = AppWatcher.config.copy(watchFragmentViews = false)
    }


    fun modulesApplicationInit() {
        for (moduleImpl in ModuleConfig.MODULE_LIST) {
            try {
                val clazz = Class.forName(moduleImpl)
                val obj = clazz.newInstance()
                if (obj is IApplication) {
                    obj.onCreate(this)
                }
            } catch (e: ClassNotFoundException) {
                e.printStackTrace()
            } catch (e: IllegalAccessException) {
                e.printStackTrace()
            } catch (e: InstantiationException) {
                e.printStackTrace()
            }
        }
    }


}